package com.dwy2002.order.impl.handler;


import com.dwy2002.order.service.IOrderProductService;
import com.dwy2002.order.service.handler.OrderTypeHandler;
import com.dwy2002.pojo.dto.order.OrderDTO;
import com.dwy2002.pojo.vo.order.OrderVO;
import com.dwy2002.product.service.IProductsService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * @author 杜伟毅
 * @version 1.0
 * @since 2025/03/11
 */
@Component("productOrderHandler")
@RequiredArgsConstructor
public class ProductOrderHandler implements OrderTypeHandler {
    private final IOrderProductService orderProductService;
    private final IProductsService productsService;

    /**
     * 订单商品处理方法
     *
     * @param orderId 订单id
     * @param dto     订单信息DTO
     * @author 杜伟毅
     * @since 2025/3/11 0011 22:39
     */
    @Override
    public void handle(String orderId, OrderDTO dto) {
        orderProductService.saveOrderProduct(orderId, dto.getOrderProducts());
        dto.getOrderProducts().forEach(p -> productsService.deductionProduct(p.getProductId(), p.getQuantity()));
    }

    /**
     * 订单商品删除方法
     *
     * @param orderId 订单id
     * @author 杜伟毅
     * @since 2025/3/13 0013 11:11
     */
    @Override
    public void deleteHandle(String orderId) {
        orderProductService.deleteOrderProduct(orderId);
    }

    /**
     * 获取订单详情方法
     *
     * @param orderVO 订单VO
     * @author 杜伟毅
     * @since 2025/3/13 0013 12:56
     */
    @Override
    public void getDetailsHandle(OrderVO orderVO) {
        orderVO.setProducts(orderProductService.getProductsByOrderId(orderVO.getId()));
    }


}
